package net.stratusprogramming.school.Game.Framework.Scene;

import net.stratusprogramming.school.Game.Framework.System.Config;
import net.stratusprogramming.school.Game.Framework.System.InstanceMgr;
import net.stratusprogramming.school.Game.Framework.System.SaveMgr;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class Camera {
	
	/* Allow the user to control the camera with WASD keys? Should be off if the camera is set to follow player. */
	private boolean allowPanning = true;
	
	private Input input;
	private Rectangle boundingBox;
	private float speed = 0.7f; // make variable in future?
	private float x; 
	private float y;
	
	
	/**
	 * Creates a new camera
	 */
	public Camera() {
		this.input = InstanceMgr.input;
		boundingBox = new Rectangle(x, y, Config.WINDOW_WIDTH , Config.WINDOW_HEIGHT);
	}
	
	/**
	 * Creates a new camera
	 * 
	 * @param allowPanning if wsad keys can pan the camera
	 */
	public Camera(boolean allowPanning) {
		this();
		this.allowPanning = allowPanning;
	}
	
	public void render(Actor actor, Graphics g) {
		// temporarily offset actor's coords for redraw
		actor.x -= x;
		actor.y -= y;
		// rendering
		if(actor.boundingBox != null) {
			// only bother rendering if bounding box is in cameras bounding box
			if(boundingBox.intersects(actor.boundingBox)) actor.render(g);
		} else actor.render(g);
		// reset coords to where they were
		actor.x += x;
		actor.y += y;
	}
	
	public void update(int delta) {
		if(allowPanning) {
			if (input.isKeyDown(Input.KEY_W)) 
				y -= speed * delta;	      
		    if (input.isKeyDown(Input.KEY_A))
		    	x -= speed * delta;
		    if (input.isKeyDown(Input.KEY_S))
		        y += speed * delta;	      
		    if (input.isKeyDown(Input.KEY_D))
		        x += speed * delta;
		}
		// update bounding box position
		boundingBox.setX(x);
		boundingBox.setY(y);
	}

	public void save(SaveMgr saveMgr) {
		saveMgr.setSetting("camera-x", x);
		saveMgr.setSetting("camera-y", y);
	}

	public void load(SaveMgr saveMgr) {
		if(saveMgr.hasSetting("camera-x")) x = saveMgr.getSettingFloat("camera-x");
		if(saveMgr.hasSetting("camera-y")) y = saveMgr.getSettingFloat("camera-y");
	}	
}
